// packageA/pages/seckill/detail.js
const app = getApp();
function t(t, o, a) {
  return o in t ? Object.defineProperty(t, o, {
    value: a,
    enumerable: !0,
    configurable: !0,
    writable: !0
  }) : t[o] = a, t;
}

let o, a = require("../../../wxParse/wxParse.js"),
  s = {
    id: "",
    num: 1,
    pro: [],
    prostr: []
  },
  i = [],
  n = [],
  r = "",
  d = "";

Page((o = {
  data: {
    timeLeft: "",//倒计时
    timer: "",//倒计时
    time_list:[],//倒计时
    hidden: !1,
    hiddenOrder: !1,
    hiddenAddress: !0,
    is_collect: 0,
    currentIndex: 1,
    currentTab: 0,
    num: 1,
    goodsComment: [],
    properties: [],
    indicatorDots: !0,
    autoplay: !0,
    interval: 4e3,
    duration: 1e3,
    showView: !0,
    scrollTop: 0,
    floorstatus: !1,
    parameteCont: [],
    isShow: false,
    goods:[]
  },

  detail:function(id,tmr){
    let that = this;
    let ru_id = app.globalData.ru_id;
    let o = wx.getStorageSync("token");
    wx.request({
      url:  app.apiUrl("seckill/detail"),
      data:{
        id: id,
        tmr: tmr,
        ru_id: ru_id
      },
      method: "POST",
      header: {
        "Content-Type": "application/json",
        "X-ECTouch-Authorization": o
      },
      success: function (res) {
        console.log('detail',res.data)
        let _imgList = [];
        for (let i = 0; i < res.data.pictures.length; i++) {
          _imgList.push(res.data.pictures[i].img_url)
        }
        that.setData({
          pictures: res.data.pictures,
          goods:res.data.goods,
          tmr:res.data.tmr,
          imgList:_imgList,
          properties:res.data.specification,
          num: res.data.goods.sec_limit,
          order_number:res.data.order_number,
        })
        a.wxParse("goods_desc", "html", res.data.goods_desc, that, 5);
        for (var key in res.data.specification){
          that.getProper(res.data.specification[key].values[0].id);
        }
        
        let end_time = (new Date(res.data.goods.formated_end_date).getTime())/1000;
        // if(res.data.tmr == '1'){
        //   end_time += 79200;
        // }
        
        that.getDateTime(end_time);//倒计时
      }
    });
  },

  // 倒计时
  getDateTime(time) {
    let that = this;
    that.data.timer = setInterval(() => { 
      that.setData({
        timeLeft: app.getTimeLeft(time * 1000)
      })
      if (that.data.timeLeft.mss <= "0") {
        clearInterval(that.data.timer);
        let obj = {
          day: "00",
          hour: "00",
          minute: "00",
          second: "00",
          mss: "0",
        }
        that.setData({
          timeLeft: obj
        });
      }
      console.log(that.data.timeLeft)
    }, 1000);
  },

  // 右侧导航
  toggle:function(){
    let that = this;
    that.setData({
      isShow:!that.data.isShow
    })
  },

  goodsCheckout: function(e) {
    let that = this,
    a = wx.getStorageSync("token");
    let properties = that.data.properties;
    let goods_spec = [];
    console.log('properties',properties)
    for (const key in properties) {
      for (let i = 0; i < properties[key].values.length; i++) {
        if (properties[key].values[i].checked) {
          goods_spec.push(properties[key].values[i].id)
        }
      }
    }
    goods_spec = goods_spec.join(',');
    console.log('goods_spec',goods_spec)
    wx.request({
      url: app.apiUrl("seckill/buy"),
      data: {
        goods_spec: goods_spec,
        sec_goods_id: s.id,
        // region_id: 2,
        // area_id:"1",
        // area_city: 604
      },
      method: "post",
      header: {
        "Content-Type": "application/json",
        "X-ECTouch-Authorization": a
      },
      success: function(res) {
        console.log('立即抢购',res.data.flow_type)
        wx.navigateTo({
          url: './checkout?flow_type=' + res.data.flow_type,
        })
      }
    });
  },
  onChangeShowState: function() {
    let t = this;
    t.setData({
      showView: !t.data.showView
    });
  },
  up: function() {
    let t = this.data.num;
    ++t >= s.num && (t = s.num), this.setData({
      num: t
    }), s.num = t;
  },
  down: function() {
    let t = this.data.num;
    --t <= 1 && (t = 1), this.setData({
      num: t
    }), s.num = t;
  },
  import: function(t) {
    let o = Math.floor(t.detail.value);
    o <= 1 && (o = 1), o >= 999 && (o = 999), this.setData({
      num: o
    }), s.num = o;
  },
  modelTap: function(event) {
    let that = this;
    let id = event.currentTarget.dataset.id;
    let specName = event.currentTarget.dataset.name;
    let _properties = that.data.properties;
    for (const key in _properties) {
      if (_properties[key].name == specName) {
        for (let i = 0; i < _properties[key].values.length; i++) {
          if (_properties[key].values[i].id == id) {
            //如果已经选中，则反选
            if (_properties[key].values[i].checked) {
              _properties[key].values[i].checked = 0;
            } else {
              _properties[key].values[i].checked = 1;
            }
          } else {
            _properties[key].values[i].checked = 0;
          }
        }
      }
    }
    that.setData({
      properties: _properties
    })
    that.getProper(id);
  },

  getProper: function(t) {
    i = [];
    n = [];
    let that = this;
    var o = that.data.properties;
    for (var a in o){
      for (var e in o[a].values){
        o[a].values[e].checked = !1,
         o[a].values[e].id == t && (s.pro[o[a].name] = t,
          s.prostr[o[a].name] = o[a].values[e].label);
      }
    }
    for (var a in o){
      if (void 0 != s.pro[o[a].name] && "" != s.pro[o[a].name]){
        for (var e in o[a].values) {
          o[a].values[e].id == s.pro[o[a].name] && (o[a].values[e].checked = !0);
        }
      }
    }
      
    for (var r in s.pro) {
      i.push(s.pro[r]);
    }
    for (var d in s.prostr) {
      n.push(s.prostr[d]);
    }
    that.setData({
      properties: o,
      selectedPro: n.join(",")
    });
  },


  addCollect: function() {
    let t = this,
      o = wx.getStorageSync("token");
    wx.request({
      url: app.apiUrl("user/collect/add"),
      data: {
        id: s.id
      },
      method: "post",
      header: {
        "Content-Type": "application/json",
        "X-ECTouch-Authorization": o
      },
      success: function(o) {
        t.setData({
          collect_list: o.data.data
        });
      }
    });
  },
  setCurrent: function(t) {
    let o = this;
    t.detail.current > 0 ? o.setData({
      currentTab: t.detail.current
    }) : o.setData({
      currentTab: 0
    }), this.setData({
      currentIndex: t.detail.current + 1
    });
  },
  swichNav: function(t) {
    let o = this;
    o.setData({
      showViewvideo: !o.data.showViewvideo
    });
  },
  imgPreview: function(event) {
    var that = this;
    var src = event.currentTarget.dataset.src;//获取data-src
    var imgList = that.data.imgList;//获取data-list
    //图片预览
    wx.previewImage({
      current: src, // 当前显示图片的http链接
      urls: imgList // 需要预览的图片http链接列表
    })
  },
  toOrder: function(t) {
    this.setData({
      hiddenOrder: !1,
      hiddenAddress: !0
    });
  },
  toAddress: function(t) {
    this.setData({
      hiddenOrder: !0,
      hiddenAddress: !1
    });
  },

  onLoad: function(options) {
    let that = this;
    console.log('onload',options)
    that.detail(options.id,options.tmr);
    s.id = options.id;
  },
  onShow: function() {
    let that = this;

    app.getUserInfo();
    if (app.globalData._linkTo) {
      wx.navigateTo({
        url: app.globalData._linkTo,
      });
    }
    app.globalData._linkTo = '';
    s.num = 1, s.pro = [];
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    var that = this;
    clearInterval(that.data.timer);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    var that = this;
    clearInterval(that.data.timer);
  },
},
// 显示返回顶部
t(o, "scroll", function(t) {
  t.detail.scrollTop > 300 ? this.setData({
    floorstatus: !0
  }) : this.setData({
    floorstatus: !1
  });
}),
//返回顶部
t(o, "goTop", function(t) {
  this.setData({
    scrollTop: 0
  });
}),
// 分享
t(o, "onShareAppMessage", function() {
  return {
    title: this.data.goods.goods_name,
    desc: "小程序本身无需下载，无需注册，不占用手机内存，可以跨平台使用，响应迅速，体验接近原生App",
    path:"../../../pages/goods/index?objectId=" + s.id
  };
}), o));